#include "DomElementEdit.h"

#include <QBoxLayout>


DomElementEdit::DomElementEdit(QWidget *parent) :
    QWidget(parent)
{
    QVBoxLayout *vLayout0 = new QVBoxLayout(this);
    QHBoxLayout *hLayout0 = new QHBoxLayout();

    mpAttributeTableBox = new QGroupBox("Attributes", this);
    QVBoxLayout *vLayout1 = new QVBoxLayout(mpAttributeTableBox);
    mpAttributeTable = new QTableWidget(mpAttributeTableBox);
    QStringList headerLables;
    headerLables << "Name" << "Value";
    mpAttributeTable->setColumnCount(2);
    mpAttributeTable->setHorizontalHeaderLabels(headerLables);
    vLayout1->addWidget(mpAttributeTable);

    hLayout0->addWidget(mpAttributeTableBox);
    hLayout0->addStretch(-1);

    vLayout0->addLayout(hLayout0);
    vLayout0->addStretch(-1);
}

void DomElementEdit::setDomNode(const QDomNode &node)
{
    mDomNode = node;

    updateAttributeTable();
}

void DomElementEdit::updateAttributeTable()
{
    if(!mDomNode.isElement())
        return;

    QDomElement element = mDomNode.toElement();
    QDomNamedNodeMap attrMap = element.attributes();
    mpAttributeTable->setRowCount(0);
    mpAttributeTable->setRowCount(attrMap.count());

    for(int i = 0; i < attrMap.count(); i++)
    {
        QDomAttr attr = attrMap.item(i).toAttr();
        mpAttributeTable->setItem(i, 0, new QTableWidgetItem(attr.name()));
        mpAttributeTable->setItem(i, 1, new QTableWidgetItem(attr.value()));
    }
}
